import PubSub from 'pubsub-js'
import request from '../../utils/request'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    playListId: '',//播单的id
    playListInfo: [],//歌单信息
    playListSongs: '',//歌单内容
    index: 0,//歌单音乐下标
    playListTitleInfo: '',//歌单标签信息
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //判断用户是否登录
    let userInfo = wx.getStorageSync('userInfo')
    if (!userInfo) {
      wx.showToast({
        title: '请先登录',
        icon: 'error',
        success: () => {
          //跳转至登录界面
          wx.reLaunch({
            url: '/pages/login/login',
          })
        }
      })
    }
    let playListId = options.playListId
    this.setData({
      playListId
    })
    //调用playListInfo函数
    this.getplayListInfo(playListId);
    //调用getplayListTitleInfo函数
    this.getplayListTitleInfo(playListId);

  },

  //获取歌单信息
  async getplayListTitleInfo(playListId) {
    let playListTitleInfoData = await request('/playlist/detail?id=' + playListId)

    let playListTitleInfo = playListTitleInfoData.playlist
    this.setData({
      playListTitleInfo
    })
  },

  //获取歌单的歌曲数据
  async getplayListInfo(playListId) {
    let playListInfo = await request(`/playlist/track/all?id=${playListId}&limit=30`)
    let playListSongs = playListInfo.songs
    // console.log(playListSongs);
    this.setData({
      playListInfo,
      playListSongs
    })
  },

  //跳转至songDetail页面
  toSongDetail(event) {
    console.log(event);
    let { song, index } = event.currentTarget.dataset
    this.setData({
      index
    })
    let { playListSongs } = this.data
    //路由跳转传参:query参数
    wx.navigateTo({
      url: '/songPackage/pages/songDetail/songDetail?index=' + index,
      success(res) {
        // 通过eventChannel向被打开页面传送数据
        res.eventChannel.emit('acceptDataFromOpenerPage', playListSongs)
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})